<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <title>css js操作</title>
        <style>
            :root{
                --primary:#333;
            }
            body{
                color:var(--primary);
            }
        </style>
    </head>
    <body>
        <h1>CSS js操作</h1>
        <p>来源:<a href="http://www.ruanyifeng.com/blog/2017/05/css-variables.html?utm_source=tuicool&utm_medium=referral">http://www.ruanyifeng.com/blog/2017/05/css-variables.html?utm_source=tuicool&utm_medium=referral</a></p>
        <div class="box"></div>
        <script>
            // 1.检测是否支持css变量
            const isSupported =
              window.CSS &&
              window.CSS.supports &&
              window.CSS.supports('--a', 0);

            if (isSupported) {
              /* supported */
            } else {
              /* not supported */
            }
            // 1.js操作css变量
            // 设置变量
            document.body.style.setProperty('--primary', '#7F583F');

            // 读取变量
            var v=document.body.style.getPropertyValue('--primary').trim();
            // '#7F583F'
            console.log('v='+v);

            // 删除变量
            setTimeout(function(){
                document.body.style.removeProperty('--primary');
                console.log('v='+document.body.style.getPropertyValue('--primary').trim());
            },1000);
        </script>
    </body>
</html>